package com.zq.service.impl;

import com.zq.entity.Tag;
import com.zq.entity.Type;
import com.zq.repository.TypeRepository;
import com.zq.service.TypeService;
import com.zq.utils.FenyeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.List;

/**
 * @Author:任人渐疏
 * @Date:Created in 11:35 上午 2020/6/27
 * Update:true
 */
@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private  TypeService typeService;
    @Autowired
    private TypeRepository typeRepository;
    @Override
    public FenyeUtil<Type> findAllList(Type type, Integer page) {
        FenyeUtil<Type> f=new FenyeUtil<>();
        PageRequest of = PageRequest.of(page - 1, 10);
        Page<Type> all = typeRepository.findAll(get(type), of);
        f.setData(all.getContent());
        f.setCount((int) all.getTotalElements());
        f.setCode(0);
        f.setMsg("");
        return f;
    }

    private Specification<Type> get(Type type) {
        return new Specification<Type>() {
            @Override
            public Predicate toPredicate(Root<Type> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate conjunction = criteriaBuilder.conjunction();
                List<Expression<Boolean>> expressions = conjunction.getExpressions();
                if(!StringUtils.isEmpty(type.getTname())){
                    expressions.add(criteriaBuilder.like(root.get("tname"),"%"+type.getTname()+"%"));
                }
                return conjunction;
            }
        };
    }

    @Override
    public String addType(Type type) {
        Type byTName = typeRepository.findByTagName(type.getTname());
        if(!StringUtils.isEmpty(byTName)){
            return "已存在，请更换类型名称进行添加";
        }else{
        try {
            typeRepository.save(type);
            return "添加成功";
        }catch (Exception e){
            return "添加失败";
        }
    }
        }
    @Override
    public String updateType(Type type) {
        Type byTName = typeRepository.findByTagName(type.getTname());
        if (!StringUtils.isEmpty(byTName)) {
            return "已存在，请更换类型名称进行修改";
        } else {
            try {
                typeRepository.saveAndFlush(type);
                return "修改成功";
            } catch (Exception e) {
                return "修改失败";
            }
        }
    }

    @Override
    public String deleteType(String tid) {
        try {
            String[] split = tid.split(",");
            for (int i = 0; i < split.length; i++) {
                Integer id=Integer.parseInt(split[i]);
                typeRepository.deleteById(id);
            }
        return "删除成功";
        }catch (Exception e){
            return "删除失败";
        }
    }
}
